//
//  IOSGraphicsContext.h
//  AirCpp
//
//  Created by Penghao on 14-3-6.
//  Copyright (c) 2014年 PengHao. All rights reserved.
//

#ifndef __AirCpp__IOSGraphicsContext__
#define __AirCpp__IOSGraphicsContext__

#include <iostream>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#include "../../AirObject/AirObject.h"

namespace AirCpp {
    class IOSGraphicsContext : public AirObject{
    private:
        CAEAGLLayer *eaglLayer;
        EAGLContext *context;
    public:
        
        int init(void *_eaglLayer){
            eaglLayer = (__bridge CAEAGLLayer *)_eaglLayer;
            eaglLayer.opaque = YES;
            eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
            context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
            if (context) {
                return 0;
            }else{
                return -1;
            }
        }
        
        int active(void){
            return (context && [EAGLContext setCurrentContext:context])?0:-1;
        }
        
        void display(void){
            [context presentRenderbuffer:GL_RENDERBUFFER];
        }
        
        int bind_to_drawable_storage(void){
            return [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:eaglLayer]?0:-1;
        }
    };
}
#endif /* defined(__AirCpp__IOSGraphicsContext__) */
